package com.aline.seo.service;

import com.aline.seo.entity.Role;
import com.aline.seo.entity.SysUser;
import com.aline.seo.entity.User;
import com.aline.seo.entity.UserRole;
import com.aline.seo.repository.UserRepository;
import com.aline.seo.repository.UserRoleRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName UserService
 * @Description
 * @Author 孟志浩
 * @Date 2019/4/9 14:55
 **/
@Service
public class UserService implements UserDetailsService {

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private UserRoleRepository userRoleRepository;


    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User u = userRepository.findByUsername(username);
        if (u == null) {
            throw new UsernameNotFoundException("用户不存在");
        }
        List<String> roles = userRoleRepository.findRoleByUserId(u.getId());
        SysUser user = new SysUser(u, roles);
        return user;
    }
}
